package com.iotechn.unimall.admin.api.articleClass;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.ArticleClassDO;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.ArticleClassMapper;
import com.iotechn.unimall.data.vo.SelectVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ArticleClassServiceImpl extends ServiceImpl<ArticleClassMapper, ArticleClassDO> implements ArticleClassService {

    @Override
    public Page<ArticleClassDO> page(String name, String code, Integer page, Integer limit, Long adminId) throws ServiceException {
        //条件查询
        QueryWrapper<ArticleClassDO> wrapper = new QueryWrapper<>();
        if (ObjectUtils.isNotEmpty(name)) {
            wrapper.lambda().like(ArticleClassDO::getName, name);
        }
        if (ObjectUtils.isNotEmpty(code)) {
            wrapper.lambda().like(ArticleClassDO::getCode, code);
        }
        wrapper.lambda().orderByAsc(ArticleClassDO::getId);
        return baseMapper.selectPage(Page.div(page, limit, ArticleClassDO.class), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArticleClassDO create(ArticleClassDO articleClassDO, Long adminId) throws ServiceException {
        List<ArticleClassDO> articleClassDOS = baseMapper.selectAllByCode(articleClassDO.getCode());
        if (ObjectUtils.isNotEmpty(articleClassDOS)) {
            throw new AdminServiceException(ExceptionDefinition.CODE_EXIST);
        }
        Date date = new Date();
        articleClassDO.setGmtCreate(date);
        if (baseMapper.insert(articleClassDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return articleClassDO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        if (baseMapper.deleteById(id) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArticleClassDO edit(ArticleClassDO articleClassDO, Long adminId) throws ServiceException {
        Date date = new Date();
        articleClassDO.setGmtUpdate(date);
        if (baseMapper.updateById(articleClassDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return articleClassDO;
    }

    @Override
    public List<SelectVO> ArticleClassSelect(Long adminId) throws ServiceException {
        List<ArticleClassDO> articleClassDOS = baseMapper.selectList(null);
        List<SelectVO> collect = articleClassDOS.stream().map(o -> {
            SelectVO selectVO = new SelectVO();
            BeanUtil.copyProperties(o, selectVO);
            return selectVO;
        }).collect(Collectors.toList());
        return collect;
    }
}
